package com.yanq.liancar.activity;
/*
 *  @创建者     Administrator
 *  @创建时间   2018/12/3  9:33
 *  @描述       ${TODO}
 */


import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.google.gson.Gson;
import com.gyf.barlibrary.ImmersionBar;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.vondear.rxtool.view.RxToast;
import com.yanq.liancar.R;
import com.yanq.liancar.activity.base.BaseToolbarActivity;
import com.yanq.liancar.adapter.ShopCommentAdapter;
import com.yanq.liancar.bean.ShopCommentBean;
import com.yanq.liancar.url.Config;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ShopCommentActivity extends BaseToolbarActivity {
    @BindView(R.id.recyclerview)
    RecyclerView mRecyclerView;
    @BindView(R.id.smartrefreshlayout)
    SmartRefreshLayout smartrefreshlayout;
    private List<ShopCommentBean.DataBean> rcsBeanList = new ArrayList<>();
    private ShopCommentAdapter mAdapter;
    private String shopID;
    private int page = 1;
    private boolean isClear;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shopcomment);
        ButterKnife.bind(this);
        ImmersionBar.with(this).statusBarDarkFont(true).statusBarColor(R.color.white).init();
        mTvTitle.setText("全部评论");
        shopID = getIntent().getStringExtra("shopID");
        initRecyclerview();
        initListener();
        getComment();
    }

    private void initRecyclerview() {
        mRecyclerView.setNestedScrollingEnabled(false);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new ShopCommentAdapter(R.layout.item_small_c_shopedetails, rcsBeanList);
        mRecyclerView.setAdapter(mAdapter);
    }

    private void initListener() {
        smartrefreshlayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                page = 1;
                isClear = true;

                getComment();
                refreshlayout.finishRefresh();
            }
        });
        smartrefreshlayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                isClear = false;
//                if (pages >= page) {
                    getComment();
//                } else {
//                    RxToast.normal("暂无更多数据");
//                }
                refreshLayout.finishLoadMore();
            }

        });
    }

    //获取评论
    private void getComment() {
        OkGo.<String>post(Config.s + "llc/shop/showAllShopRemark")
                .tag(this)
                .params("sid", shopID)
                .params("type", 0)
                .params("page", page)
                .params("num", 10 + "")
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        Gson gson = new Gson();
                        ShopCommentBean bean = gson.fromJson(response.body(), ShopCommentBean.class);
                        if (bean.isIsSuccess()) {
                            if (isClear){
                                rcsBeanList.clear();
                            }
                            rcsBeanList.addAll(bean.getData());
                            mAdapter.notifyDataSetChanged();
                        } else {
                            RxToast.error(bean.getOutputMessage());
                        }
                    }
                });
    }
}
